{
LANG : PASCAL
PROB : maze1
}

program MAZE1;
const
  fi = 'maze1.in';
  fo = 'maze1.out';
  bit : array[1..4] of byte = (1, 2, 4, 8);
  dx : array[1..4] of shortint = (-1, 0, 0, 1);
  dy : array[1..4] of shortint = (0, -1, 1, 0);
  maxq = 10000;
type
  point = record
            x, y : shortint;
          end;
var
  a : array[1..201, 1..77] of char;
  c : array[0..101, 0..39, 1..4] of boolean;
  d : array[0..101, 0..39] of integer;
  q : array[1..maxq] of point;
  n, m, l, r : longint;

procedure inp;
var
  i, j : integer;
begin
  readln(n, m);
  fillchar(a, sizeof(a), ' ');
  for i := 1 to 2*m+1 do
  begin
    for j := 1 to 2*n+1 do
      if not eoln then read(a[i, j])
      else break;
    readln;
  end;
end;

procedure init_graph;
var
  i, j, k, x, y, x1, y1 : integer;
begin
  fillchar(c, sizeof(c), false);
  for i := 1 to m do
    for j := 1 to n do
    begin
      x := i*2;
      y := j*2;
      for k := 1 to 4 do
      begin
        x1 := x + dx[k];
        y1 := y + dy[k];
        if a[x1, y1] = ' ' then
        begin
          c[i, j, k] := true;
          c[i+dx[k], j+dy[k], 5-k] := true;
        end;
      end;
    end;
end;

procedure initBFS;
var
  i, j : integer;
begin
  l := 1;
  r := 0;
  fillchar(d, sizeof(d), $30);
  for i := 1 to m do
  begin
    if c[i, 0, 3] then
    begin
      inc(r);
      q[r].x := i;
      q[r].y := 0;
      d[i, 0] := 0;
    end;
    if c[i, n+1, 2] then
    begin
      inc(r);
      q[r].x := i;
      q[r].y := n+1;
      d[i, n+1] := 0;
    end;
  end;
  for j := 1 to n do
  begin
    if c[0, j, 4] then
    begin
      inc(r);
      q[r].x := 0;
      q[r].y := j;
      d[0, j] := 0;
    end;
    if c[m+1, j, 1] then
    begin
      inc(r);
      q[r].x := m+1;
      q[r].y := j;
      d[m+1, j] := 0;
    end;
  end;
end;

procedure tang(var k : longint);
begin
  if k = maxq then k := 1
  else inc(k);
end;

procedure process;
var
  i : integer;
  u, v : point;
begin
  initBFS;
  while true do
  begin
    u := q[l];
    for i := 1 to 4 do
      if c[u.x, u.y, i] then
      begin
        v.x := u.x + dx[i];
        v.y := u.y + dy[i];
        if d[v.x, v.y] > d[u.x, u.y] + 1 then
        begin
          d[v.x, v.y] := d[u.x, u.y] + 1;
          tang(r);
          q[r] := v;
        end;
      end;
    if l = r then break;
    tang(l);
  end;
end;

procedure out;
var
  i, j : integer;
  maximum : longint;
begin
  maximum := 0;
  for i := 1 to m do
    for j := 1 to n do
      if maximum < d[i, j] then maximum := d[i, j];
  writeln(maximum);
end;

begin
  assign(input, fi); reset(input);
  assign(output, fo); rewrite(output);
  inp;
  init_graph;
  process;
  out;
  close(output);
  close(input);
end.
